/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cronus.client;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 *
 * @author mark
 */
public class MNewBackup {
    
    private PrintWriter pw;
    private String dateTime;
    private String rapporten = "";
    private String gebruikers = "";
    private int i = 1;
    private String e;
    private String w;
    private String a;
    private String r;
    private String rn;
    
    public MNewBackup() {
        
        DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss");
        Date date = new Date();
        this.dateTime = dateFormat.format(date);
        
        try {

            pw = new PrintWriter(new FileWriter("backup\\" + dateTime + ".txt"));
            
            SocketConnection socketcon = new SocketConnection();
            
            String query = "SELECT * From rapporten";
            
            socketcon.send(query);
            
            Object[][] uitkomst = socketcon.read();
            
            for(Object[] ao : uitkomst){
                for(Object o : ao ){
                    rapporten = "" + (o.toString());
                    
                    if (i == 1){
                        rn = rapporten;
                        i = 2;
                    }
                        else if (i == 2){
                            r = rapporten;
                            pw.println("INSERT INTO rapporten (rapportnaam, rapport) values('" + rn + "','" + r + "')");
                            i = 1;
                        }
                }
            }
            
            SocketConnection socketcon2 = new SocketConnection();
            
            String query2 = "SELECT * From inloggegevens";
            
            socketcon2.send(query2);
            
            Object[][] uitkomst2 = socketcon2.read();
            
            for(Object[] ao : uitkomst2){
                for(Object o : ao){
                    gebruikers = "" + (o.toString());
                    
                    if (i == 1){
                        e = gebruikers;
                        i = 2;
                    }
                        else if (i == 2){
                            w = gebruikers;
                            i = 3;
                        }
                            else if (i == 3){ 
                                a = gebruikers;
                                pw.println("INSERT INTO inloggegevens (email, wachtwoord, admin) values('" + e + "','" + w + "','" + a + "')");
                                i = 1;
                            }
                    
                }
            }
            
            
        }
        catch (IOException e){
            System.err.println("Caught IOException: " +  e.getMessage());
        }
        finally {
         if (pw != null) {
             JOptionPane.showMessageDialog(null,"Backup aangemaakt","Geslaagd",JOptionPane.INFORMATION_MESSAGE);
             pw.close();
          } 
    }
    
    }
}
